package com.kpx.app.fragment;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.adapter.MyPagerAdapter;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.base.FragmentBase;
import com.kpx.app.bean.HelpBean;
import com.kpx.app.cache.FileCache;
import com.kpx.app.configs.Constants;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.AnimationsUtils;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.uiutils.DisplayUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.ViewGrowth;
import com.kpx.app.view.ViewShowField;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/20.
 * 成长
 */
public class FragmentGrowth extends FragmentBase {
    /**
     * 上一个内容部分
     */
    protected int before;
    /**
     * 右上角的popupwindow
     */
    private PopupWindow popupWindow;
    /**
     * 用于装内容页面
     */
    private List<View> listViews= new ArrayList<View>();
    /**
     * 上一个所选择的菜单项页面
     */
    private int before_pager;
    private TextView txt_filter;
    private RadioGroup group_task;
    private ViewPager vp_content;
    private ViewShowField vshowfield;
    private ViewGrowth vgrowth;
    private ArrayList<HelpBean> helps;

    @Override
    public int setLayout() {
        return R.layout.fragment_growth;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();// 初始化view
        getHelp();
    }

    @Override
    public void onResume() {
        super.onResume();
        helps = (ArrayList<HelpBean>) FileCache.sharedInstance().get(Constants.KEY_HELP_LIST);
        if (helps != null) {
            listViews.clear();
            for (int i = 0; i < helps.size(); i++) {
                vshowfield = new ViewShowField(getActivity());
                vshowfield.setData(helps.get(i));
                listViews.add(vshowfield);
            }
            vp_content.setAdapter(new MyPagerAdapter(listViews));// 设置viewpager的适配器
            vp_content.setCurrentItem(before);// 设置默认位置
        } else {
            getHelp();
        }
    }

    private void getHelp() {
        if(getActivity()==null){
            return;
        }
        showLoadingDialog(getActivity());
        MyHashMap map = new MyHashMap();
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                dissmissLoadingDialog(getActivity());
                if (result.getCode() == 0) {
                    helps = (ArrayList<HelpBean>) result.getData();
                    if (helps != null) {
                        FileCache.sharedInstance().put(Constants.KEY_HELP_LIST, helps);
                        if (helps != null) {
                            listViews.clear();
                            group_task.removeAllViews();
                            for (int i = 0; i < helps.size(); i++) {
                                vshowfield = new ViewShowField(getActivity());
                                vshowfield.setData(helps.get(i));
                                listViews.add(vshowfield);
                                RadioButton b = (RadioButton) View.inflate(getActivity(),R.layout.radio_button_growth_show,null);
                                b.setId(i);
                                b.setText(helps.get(i).getName());
                                b.setChecked(i==0);
                                group_task.addView(b);
                            }
                            vp_content.setAdapter(new MyPagerAdapter(listViews));// 设置viewpager的适配器
                            vp_content.setCurrentItem(before);// 设置默认位置
                        }
                    }
                } else {
                    ToastUtil.getInstance().showToast(getActivity(), result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                dissmissLoadingDialog(getActivity());
                ToastUtil.getInstance().showToast(getActivity(), error);
            }

        }, HelpBean.class, HttpDataRequest.TYPE_LIST).POST(HttpDataReqUrl.URL_POST_HELP, true, map);

    }


    /**
     * 初始化view
     *
     * @version 1.0
     * @createTime 2015年1月4日, 下午5:38:25
     * @updateTime 2015年1月4日, 下午5:38:25
     * @createAuthor chencong
     * @updateAuthor chencong
     * @updateInfo (此处输入修改内容, 若无修改可不写.)
     */
    private void initView() {
        // 初始化view-------------------------------------------
        txt_filter = (TextView) rootView.findViewById(R.id.txt_filter);
        // 迪讯的菜单
        group_task = (RadioGroup) rootView.findViewById(R.id.group_task);
        // 迪讯内容的容器
        vp_content = (ViewPager) rootView.findViewById(R.id.vp_content);

        // 初始化选项-------------------------------------------
        before_pager = R.id.radio_dating;// 默认初始显示第一个页面
        before = 0;// 设置初始选择的菜单

        vp_content.setOnPageChangeListener(new MyOnPageChangeListener());// 设置viewpager的监听事件
        registerEvent(getActivity());// 设置点击事件
    }

    /**
     * 注册菜单的选中事件
     *
     * @param context
     * @version 1.0
     * @createTime 2014年12月31日, 上午10:58:41
     * @updateTime 2014年12月31日, 上午10:58:41
     * @createAuthor chencong
     * @updateAuthor chencong
     * @updateInfo (此处输入修改内容, 若无修改可不写.)
     */
    private void registerEvent(final Context context) {
        group_task.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                ((ActivityBase) getActivity()).hideSoftKeyboard();
                vp_content.setCurrentItem(checkedId);
            }
        });
    }

    /**
     * 用于监听页面的滑动
     *
     * @author chencong
     * @Description TODO
     * @date 2015年1月10日
     * @Copyright: Copyright (c) 2015 Shenzhen Tentinet Technology Co., Ltd. Inc. All rights reserved.
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            group_task.check(arg0);// 设置指定位置的选中状态
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
